//
//  UserCenterView.m
//  xpkc
//
//  Created by TopSage on 15/10/16.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "UserCenterView.h"
#import "UserCenterMenuButton.h"
#import "UIView+Frame.h"
@interface UserCenterView (){
    UIView *topView;
    UIScrollView *menuScroll;
    UIButton *signOutBtn;
    UIButton *setBtn;
    
}

@end

@implementation UserCenterView

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView{
    
    CGRect frame_top;
    CGRect frame_set;
    CGRect frame_menuScroll;
    CGRect frame_signOut;
    CGRect frame_header;
    
    CGFloat offset = 0.04*SCREEN_WIDTH;
    
    if (isPad) {
        //FIXME:pad适配另算
        frame_top           = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/3);
        frame_set           = CGRectMake(SCREEN_WIDTH - 60, 50, 50, 50);
        frame_menuScroll    = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40);
        frame_signOut       = CGRectMake(offset, 0, SCREEN_WIDTH-offset*2, offset*3);
        frame_header        = CGRectMake(SCREEN_WIDTH/2 - 0.09*SCREEN_WIDTH, 30, 0.18*SCREEN_WIDTH, 0.18*SCREEN_WIDTH+offset*2/3);
    }else{
        frame_top           = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH/3);
        frame_set           = CGRectMake(SCREEN_WIDTH - 45, 40, 30, 30);
        frame_menuScroll    = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 40);
        frame_signOut       = CGRectMake(offset, 0, SCREEN_WIDTH-offset*2, offset*3);
        frame_header        = CGRectMake(SCREEN_WIDTH/2 - 0.09*SCREEN_WIDTH, 30, 0.18*SCREEN_WIDTH, 0.18*SCREEN_WIDTH+offset*2/3);
    }
    
    menuScroll = [[UIScrollView alloc]initWithFrame:frame_menuScroll];
    menuScroll.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.2];
    menuScroll.showsVerticalScrollIndicator = NO;
    menuScroll.delegate = self;
    [self addSubview:menuScroll];
    
    topView = [[UIView alloc]initWithFrame:frame_top];
    topView.backgroundColor = GreenColor;
    topView.clipsToBounds = YES;
    [self addSubview:topView];
    
    UIImageView *topBGView = [[UIImageView alloc]initWithFrame:CGRectMake(-offset, -offset, SCREEN_WIDTH+offset*2, frame_top.size.height+offset*2)];
    topBGView.image = [UIImage imageNamed:@"my_topbg_color"];
    [topView addSubview:topBGView];
    if (IsIOS7) {
        //iOS7之后才有的视差滚动方法
        UIInterpolatingMotionEffect *motioneffectx = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
        motioneffectx.minimumRelativeValue = [NSNumber numberWithInt:-offset];
        motioneffectx.maximumRelativeValue = [NSNumber numberWithInt:offset];
        UIInterpolatingMotionEffect *motioneffecty = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
        motioneffecty.minimumRelativeValue = [NSNumber numberWithInt:-offset];
        motioneffecty.maximumRelativeValue = [NSNumber numberWithInt:offset];
        [topBGView addMotionEffect:motioneffectx];
        [topBGView addMotionEffect:motioneffecty];
    }
    
    _header = [[UserCenterHeadButton alloc]initWithFrame:frame_header];
    _header.buttonImage.clipsToBounds = YES;
    _header.buttonImage.image = [UIImage imageNamed:@"portrait"];
    [_header addTarget:self action:@selector(headClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_header];
    
    setBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    setBtn.frame = frame_set;
    [setBtn setImage:[UIImage imageNamed:@"my_set"] forState:UIControlStateNormal];
    [setBtn addTarget:self action:@selector(setClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:setBtn];
    
    signOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    signOutBtn.frame = frame_signOut;
    signOutBtn.backgroundColor = OrangeColor;
    [signOutBtn setTitle:@"安全退出" forState:UIControlStateNormal];
    signOutBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    signOutBtn.titleLabel.shadowOffset = CGSizeMake(1, 1);
    signOutBtn.titleLabel.shadowColor = [UIColor whiteColor];
    [signOutBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [signOutBtn addTarget:self action:@selector(signOutClick:) forControlEvents:UIControlEventTouchUpInside];
    signOutBtn.hidden = YES;
    [menuScroll addSubview:signOutBtn];
    
}

- (void)setIsLogin:(BOOL)isLogin{
    _isLogin = isLogin;
    if (isLogin) {
        signOutBtn.hidden = NO;
        menuScroll.contentSize = CGSizeMake(SCREEN_WIDTH, signOutBtn.bottom + 30);
    }else{
        signOutBtn.hidden = YES;
        menuScroll.contentSize = CGSizeMake(SCREEN_WIDTH, signOutBtn.top + 20);
        _header.buttonTitle.text = @"请登录";
        _header.buttonImage.image = [UIImage imageNamed:@"portrait"];
    }
}
- (BOOL)isIsLogin{
    return _isLogin;
}

- (void)menuTitles:(NSArray *)titles andIcons:(NSArray *)icons{
    
    CGFloat totalHeight = SCREEN_WIDTH/3;
    
    for (int i = 0; i<titles.count; i++) {
        NSString *title = [titles objectAtIndex:i];
        NSString *icon = [icons objectAtIndex:i];
        if (title.length > 0) {
            //按钮
            UserCenterMenuButton *menu = [[UserCenterMenuButton alloc]initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, 50)];
            menu.tag = i;
            menu.backgroundColor = [UIColor whiteColor];
            [menu addTarget:self action:@selector(menuClick:) forControlEvents:UIControlEventTouchUpInside];
            menu.buttonImage.image = [UIImage imageNamed:icon];
            menu.buttonTitle.text = title;
            [menuScroll addSubview:menu];
            totalHeight += 50;
        }else{
            //间隔
            UIView *sepreTop  = [[UIView alloc]initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, 0.5)];
            sepreTop.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.1];
            [menuScroll addSubview:sepreTop];
            
            UIView *sepreBottom  = [[UIView alloc]initWithFrame:CGRectMake(0, totalHeight+9, SCREEN_WIDTH, 0.5)];
            sepreBottom.backgroundColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.1];
            [menuScroll addSubview:sepreBottom];
            
            totalHeight += 10;
        }
    }
    UIView *bottom = [[UIView alloc]initWithFrame:CGRectMake(0, totalHeight, SCREEN_WIDTH, 400)];
    bottom.backgroundColor = [UIColor whiteColor];
    [menuScroll addSubview:bottom];
    //退出
    signOutBtn.top = totalHeight + 20;
    [menuScroll bringSubviewToFront:signOutBtn];
    menuScroll.contentSize = CGSizeMake(SCREEN_WIDTH, signOutBtn.top);
    logdebug(@"%f---%f",menuScroll.height,menuScroll.contentSize.height);
}

- (void)updateBtnAtIndex:(NSInteger)index withBadgevalue:(NSString *)badgevalue{
    for (UIView *view in menuScroll.subviews) {
        if (view.class == [UserCenterMenuButton class]) {
            UserCenterMenuButton *btn = (UserCenterMenuButton *)view;
            if (btn.tag == index) {
                btn.badgeValue = badgevalue;
            }
        }
    }
}

#pragma mark - scroll delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    logdebug(@"%f",scrollView.contentOffset.y);
    CGFloat scrol = scrollView.contentOffset.y/-1334 + 1;
    if (scrollView.contentOffset.y < 0) {
        topView.height = SCREEN_WIDTH/3-scrollView.contentOffset.y/3;
    }else{
        topView.height = SCREEN_WIDTH/3-scrollView.contentOffset.y/3;
        setBtn.top = 40 - scrollView.contentOffset.y/3;
        _header.transform = CGAffineTransformMakeScale(scrol, scrol);
        _header.top = 30 - scrollView.contentOffset.y/6;
        
    }
    //FIXME:顶部视图的缩放
    //TODO:顶部视图的背景图及背景色，三层视图缩放及顶层透明度变化
}

#pragma mark - 响应的代理方法
- (void)headClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(userHeadClick:)]) {
        [self.delegate userHeadClick:btn];
    }
}
- (void)menuClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(menuBtnClick:)]) {
        [self.delegate menuBtnClick:btn];
    }
}
- (void)signOutClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(signOutBtnClick:)]) {
        [self.delegate signOutBtnClick:btn];
    }
}
- (void)setClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(setBtnClick:)]) {
        [self.delegate setBtnClick:btn];
    }
}

@end
